var Post = require('../mongo/mongo').Post;
var CommentModel = require('./Comment');


module.exports = {
    create: function create(post){
        return Post.create(post).exec();
    },
    edit: function edit(postId, author, data){
        return Post.update({ author: author, _id: postId }, { $set: data }).exec();
    },
    // 通过用户 id 和文章 id 删除一篇文章
    removePost: function removePost(postId, author) {
        return Post.remove({ author: author, _id: postId })
            .exec()
            .then(function (res) {
                // 文章删除后，再删除该文章下的所有留言
                if (res.result.ok && res.result.n > 0) {
                    return CommentModel.delCommentsByPostId(postId);
                }
            });
    },
    getPosts: function getPosts(options){
        var query = {}, condition = {};
        var start = parseInt(options.start || 0);
        var count = parseInt(options.count || 20);
        if (options.author) {
            query.author = options.author;
            condition = {author: options.author};
        }
        if (options.keywords){
            query.$or = [ //多条件，数组
                {title: {$regex : options.keywords}},
                {abstract: {$regex : options.keywords}},
                {content: {$regex : options.keywords}}
            ]
            condition.$or = query.$or;
        }

        // return Post
        //     .find(query)
        //     .populate({ path: 'author', model: 'User' })
        //     .sort({ _id: -1 })
        //     .skip(start)
        //     .limit(count)
        //     .addCreatedAt()
        //     // .addCommentsCount()
        //     .exec();
        return Promise.all([
            Post
                .find(query)  
                .select({'author': 1, 'tags': 1, 'title': 1, 'cover': 1, 'abstract': 1, 'pv': 1 }) //控制返回的字段
                .populate({ path: 'author', model: 'User' })
                .sort({ _id: -1 })
                .skip(start)
                .limit(count)
                .addCreatedAt()
                // .addCommentsCount()
                .exec(),
            Post.count(condition)    
        ])
            .then(function(res){
                return res;
            })
            .catch(function(err){
                return false;
            });
    },
    getFilterPosts: function(options){
        var start = parseInt(options.start || 0);
        var count = parseInt(options.count || 20);
        var type = options.type || 'new';

        var sortOps = {}
        switch(type){
            case 'new':
                sortOps._id = -1;
                break;
            case 'hot':
                sortOps.pv = -1;
                break;
        }

        return Post
                    .find({})
                    .select({ title: 1})  //控制只返回返回title字段
                    .sort(sortOps)
                    .skip(start)
                    .limit(count)
                    .exec();
    },
    getPostById: function getPostById(postId){
        return Post
                .findOne({ _id: postId })
                .populate({ path: 'author', model: 'User' })
                .addCreatedAt()
                .exec();
    },
    addPv: function addPv(postId){
        return Post
                .update({_id: postId}, { $inc: {pv: 1}})
                .exec();
    }
}

